import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/common/department_user_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_target_tasking_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/task_allocation_hazard_investigation/task_allocation_allocated/task_allocation_allocated_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///任务分配待分配
class TaskAllocationAllocatedPage extends StatelessWidget {
  TaskAllocationAllocatedPage({super.key});

  final controller = Get.put(TaskAllocationAllocatedController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            body: Stack(
              children: [
                Container(
                  color: AppColors.bg_color_grey,
                  padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w, bottom: 50.w),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Expanded(
                            child: _getSearchEnterWidget(),
                          ),
                          AppGaps.hGap8,
                          _screen(),
                          AppGaps.vGap10,
                        ],
                      ),
                      AppGaps.vGap12,
                      Expanded(
                          child: CustomEasyRefreshList<GetTargetTaskingEntity>(
                        key: controller.globalKey,
                        dataLoad: controller.getTaskListAPP,
                        itemBuilder: (item, index) {
                          return CustomTitleChartPage(
                            title: item.troubleshootContent,
                            checkbox: _colorfulCheckBox(item),
                            titleRightWidget: _title(item, '添加', Colors.white),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                item.workKindLabel == '季节性排查' || item.workKindLabel == '重点时段及节假日前排查'
                                    ? Row(
                                        children: [
                                          _rowData(mainAxisAlignment: MainAxisAlignment.start, '${item.workKindLabel}  |  ', ''),
                                          Expanded(
                                              child: Row(
                                            children: item.workKindCycleChangeList != null
                                                ? item.workKindCycleChangeList!
                                                    .map((e) => _rowData(
                                                        mainAxisAlignment: MainAxisAlignment.start,
                                                        '${e.checkCycle ?? '空'}${e.checkCycleUnitLabel ?? '空'} ',
                                                        ''))
                                                    .toList()
                                                : [],
                                          ))
                                        ],
                                      )
                                    : _rowData('${item.workKindLabel}  |  ${item.checkCycle ?? '空'}${item.checkCycleUnitLabel ?? '空'}', ''),
                                AppGaps.vGap12,
                                _rowData('部门：', item.allocationName ?? '空'),
                                AppGaps.vGap12,
                              ],
                            ).clickOnce(() {
                              controller.onClickDetail(item.id);
                            }),
                          );
                        },
                      )),
                    ],
                  ),
                ),
                Positioned(
                    bottom: 0,
                    right: 0,
                    left: 0,
                    child: Container(
                      color: Colors.white,
                      child: Row(
                        children: [
                          AppGaps.hGap36,
                          Text(
                            '已选择  ${controller.addCount}  个',
                            style: TextStyle(color: const Color(0xFF5C6DFF), fontSize: 16.sp, fontWeight: FontWeight.w400),
                          ),
                          const Spacer(),
                          Stack(
                            alignment: Alignment.center,
                            children: [
                              SvgPicture.asset(
                                AppImages.icon_task_assigned_bg,
                                // ignore: deprecated_member_use
                                color: const Color(0xFF5C6DFF),
                                height: 50.w,
                              ),
                              Text(
                                '确 定',
                                style: TextStyle(color: Colors.white, fontSize: 15.sp, fontWeight: FontWeight.w500),
                              ),
                            ],
                          ).clickOnce(() {
                            //跳界面前判断下是否选择了数据
                            if (controller.measuresTaskIdsCount.isEmpty) {
                              AppToast.showWarning('待分配没选择~');
                              return;
                            }
                            //跳转页面选择部门
                            Get.toNamed(AppRoutes.departmentUserTreePage)?.then((value) {
                              //如果不为空则设置部门信息
                              if (value != null) {
                                final DepartmentUserTreeEntity selectedEntity = value as DepartmentUserTreeEntity;
                                // final String selected = value as String;
                                controller.addAllocationBaseMeasures(selectedEntity);
                              }
                            });
                          }),
                        ],
                      ),
                    ))
              ],
            ), // 右侧筛选栏
            // endDrawer: CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       RegionalOrganizationTreePage(
            //         selectedEntity: controller.selectedEntity,
            //         onCallBack: (RiskTreeEntity entity) {
            //           controller.selectedEntity = entity;
            //         },
            //       )
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX),
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '搜索任务名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _title(GetTargetTaskingEntity item, String title, Color color) {
    return Stack(
      alignment: Alignment.center,
      children: [
        SvgPicture.asset(
          AppImages.icon_task_assigned_bg,
          // ignore: deprecated_member_use
          color: const Color(0xFF5C6DFF),
        ),
        Text(
          title,
          style: TextStyle(
            color: color,
            fontSize: 10.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
      ],
    ).clickOnce(() {
      //添加
      item.checkboxSelected = !item.checkboxSelected;
      if (item.checkboxSelected) {
        controller.addCount++;
        controller.measuresTaskIdsCount.add(item.id!);
      } else {
        controller.addCount--;
        controller.measuresTaskIdsCount.remove(item.id!);
      }
      controller.isBool();
    });
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }

  Widget _colorfulCheckBox(GetTargetTaskingEntity item) {
    return Theme(
      data: ThemeData(unselectedWidgetColor: const Color(0xFFDCDCDC)),
      child: SizedBox(
        width: 16.w,
        height: 16.w,
        child: Checkbox(
            checkColor: const Color(0xFF3B62FF),
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
            value: item.checkboxSelected,
            onChanged: (value) {
              item.checkboxSelected = !item.checkboxSelected;
              if (item.checkboxSelected) {
                controller.addCount++;
                controller.measuresTaskIdsCount.add(item.id!);
              } else {
                controller.addCount--;
                controller.measuresTaskIdsCount.remove(item.id!);
              }
              controller.isBool();
            }),
      ),
    );
  }

}
